package com.zjp.aop.service;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.framework.ProxyFactory;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * @see AnnotationAwareAspectJAutoProxyCreator
 */
public class ProxyService {


    public static void main(String[] args) {

        ProxyService.data();

    }


    public static void data() {
        TestService testService = new TestService() {
            @Override
            public void test() {
                System.out.println("test()");
            }
        };

        ProxyFactory proxyFactory = new ProxyFactory(testService);
        proxyFactory.addAdvice((MethodBeforeAdvice) (method, args, target) -> {

        });
        proxyFactory.addAdvice((MethodBeforeAdvice) (method, args, target) -> {

        });
        proxyFactory.addAdvice(new MethodInterceptor() {
            @Nullable
            @Override
            public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {

                Object proceed = invocation.proceed();

                return proceed;
            }
        });

        TestService proxy = (TestService) proxyFactory.getProxy();

        proxy.test();
    }

}
